<template>
  <div id="building">
    <el-row type="flex" justify="center" align-items="center" class="loginTop">
      <el-col :span="8">
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <div>
          <center>
            <h1>医院信息管理系统</h1>
          </center>
          <el-input v-model="username" prefix-icon="el-icon-user-solid" placeholder="请输入账号"></el-input>
          <p></p>
          <el-input
            type="password"
            v-model="password"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
          ></el-input>
          <p></p>
          <center>
            <el-button @click="login" type="primary">登录</el-button>
            <el-button @click="reset" type="primary">重置</el-button>
          </center>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { login as lg } from "@/api/login.js";
import {
  setToken,
  removeToken,
  setList,
  getList,
  getUserId,
  setUserId,
  setUserName
} from "@/utils/LocalStorage.js";

export default {
  data() {
    return { username: "", password: "" };
  },
  methods: {
    login() {
      let user = { username: this.username, password: this.password };
      lg(user).then(response => {
        if (response.data.code == 0) {
          this.$message({
            message: "登录成功",
            type: "success"
          });
          // 使用localStorage 存储数据
          setToken(response.data.data.token);
          setList(JSON.stringify(response.data.data.result));
          setUserId(JSON.stringify(response.data.data.userId));
          setUserName(JSON.stringify(this.username));
          this.$router.push("/layout");
        } else {
          removeToken();
          this.$message.error("用户名或密码错误");
        }
      });
    },
    reset() {
      this.username = "";
      this.password = "";
    }
  }
};
</script>

<style>
#building {
  background: url("../assets/img/BJ.jpg");
  width: 100%;
  height: 100%;
  position: fixed;
  background-size: 100% 100%;
}
.loginTop {
  align-items: center;
}
</style>